package com.ext.aic.utils;

import android.text.TextUtils;

import com.ext.aic.beans.DebugAccount;
import com.ext.aic.beans.UserInfo;
import com.ext.aic.constants.ApiConstants;
import com.eyinfo.android_pure_utils.utils.JsonUtils;
import com.eyinfo.mpkv.MPLocalKV;

import lombok.Getter;

public class UserUtils {
    private static volatile UserUtils instance;

    public static UserUtils getInstance() {
        if (instance == null) {
            synchronized (UserUtils.class) {
                if (instance == null) {
                    instance = new UserUtils();
                }
            }
        }
        return instance;
    }

    private UserInfo userInfo;
    @Getter
    private boolean isEnableDebugAccount = false;
    @Getter
    private String formalPassword = "r54VmfYv";
    @Getter
    private DebugAccount debugAccount = new DebugAccount("36585@aic.com", "123456", "08c8c082-5cfa-43e2-b4c7-102642e8297b");

    public UserInfo getUserInfo() {
        if (userInfo == null || TextUtils.isEmpty(userInfo.getUserId())) {
            String rawJson = MPLocalKV.getInstance().getString(getUserCacheKey(), "");
            userInfo = JsonUtils.parseT(rawJson, UserInfo.class);
            return userInfo == null ? new UserInfo() : userInfo;
        }
        return userInfo;
    }

    private String getUserCacheKey() {
        int code = ApiConstants.baseUrlForBusiness.hashCode();
        return String.format("11c4e6086092a63b_%s", code);
    }

    private void copyUser(UserInfo source, UserInfo target) {
        if (!TextUtils.isEmpty(source.getUserId())) {
            target.setUserId(source.getUserId());
        }
        if (!TextUtils.isEmpty(source.getUserName())) {
            target.setUserName(source.getUserName());
        }
        if (!TextUtils.isEmpty(source.getNick())) {
            target.setNick(source.getNick());
        }
        if (!TextUtils.isEmpty(source.getAccessToken())) {
            target.setAccessToken(source.getAccessToken());
        }
        if (!TextUtils.isEmpty(source.getRefreshToken())) {
            target.setRefreshToken(source.getRefreshToken());
        }
        if (source.getExpiresIn() > 0) {
            target.setExpiresIn(source.getExpiresIn());
        }
        if (source.getLoginTime() > 0) {
            target.setLoginTime(source.getLoginTime());
        }
    }

    public void saveUserInfo(UserInfo userInfo) {
        if (userInfo == null || TextUtils.isEmpty(userInfo.getUserId())) {
            return;
        }
        UserInfo info = getUserInfo();
        copyUser(userInfo, info);
        String json = JsonUtils.toJson(info);
        MPLocalKV.getInstance().put(getUserCacheKey(), json);
        this.userInfo = info;
    }

    public String getUserId() {
        UserInfo userInfo = getUserInfo();
        return userInfo.getUserId();
    }

    public String getAccessToken() {
        UserInfo userInfo = getUserInfo();
        return userInfo.getAccessToken();
    }

    public boolean isLogin() {
        UserInfo userInfo = getUserInfo();
        if (userInfo == null) {
            return false;
        }
        String accessToken = userInfo.getAccessToken();
        return !TextUtils.isEmpty(accessToken);
    }

    public void clear() {
        MPLocalKV.getInstance().remove("userCacheKey");
        userInfo = null;
    }
}
